package com.deco.back.controller.fix;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deco.domain.vo.store.StoreShopVo;
import com.deco.domain.vo.sys.ChinastatesVo;
import com.deco.service.store.StoreShopService;
import com.deco.service.sys.ChinastatesService;
import com.free.common.vo.PageResponseVo;
import com.free.common.vo.PageSearchVo;
import com.free.common.vo.ResultModel;
import com.free.ext.web.spring.annoation.PageSearch;

/**
 * 广告管理 Controller
 * @author lengy
 *
 */
@Controller
public class StoreShopController {
    @Autowired
    private StoreShopService storeShopService;
    
    @Autowired
    private ChinastatesService chinastatesService;
    
	 /**
   * View视图
   */
  @RequestMapping(value="/store_shop_list")
  public ModelAndView getListView(StoreShopVo storeShopVo) {
      ModelAndView modelAndView = new ModelAndView();
      modelAndView.setViewName("fix/storeshoplist");
      if(storeShopVo.getType() == null){
    	  storeShopVo.setType(0);
    	  modelAndView.addObject("pageTitle", "经销商网络查询");
      }
      else{
    	  modelAndView.addObject("pageTitle", "快修店网络查询");
      }
      
      Map<String,ChinastatesVo> chinastatesMap =  chinastatesService.getAll();
      modelAndView.addObject("areaMap", chinastatesMap);
      modelAndView.addObject("obj", storeShopVo);
      return modelAndView;
  }
  
  @RequestMapping(value= "/store_shop_list_page")
  @ResponseBody
  public PageResponseVo<StoreShopVo> getList(@PageSearch PageSearchVo<StoreShopVo> condition){
	   PageResponseVo<StoreShopVo> storeShopVoPageInfo = storeShopService.pageInfo(condition);
	   List<StoreShopVo> storeShopVoList =  storeShopVoPageInfo.getList();
	   
	   if(storeShopVoList != null && storeShopVoList.size() > 0){
		   Map<String,ChinastatesVo> chinastatesMap =  chinastatesService.getAll();
		   for(StoreShopVo storeShop : storeShopVoList){
			   ChinastatesVo p = chinastatesMap.get(storeShop.getProv());
			   if(p != null){
				   storeShop.setProvName(p.getName());
				   ChinastatesVo c = p.getChildMap().get(storeShop.getCity());
				   if(c != null){
					   storeShop.setCityName(c.getName());
					   ChinastatesVo area = c.getChildMap().get(storeShop.getArea());
					   if(area != null){
						   storeShop.setAreaName(area.getName());
					   }
					   else{
						   storeShop.setAreaName("");
					   }
					  
				   }
				   else{
					  
					   storeShop.setCityName("");					   
					   storeShop.setAreaName("");
				   }
			   }
			   else{
				   storeShop.setProvName("");
				   storeShop.setCityName("");
				   
				   storeShop.setAreaName("");
			   }
			  
		   }
	   }
	   return storeShopVoPageInfo;
  }
  
  @RequestMapping(value = "/store_shop_edit")
  public ModelAndView edit(Integer id,Integer type){
	  StoreShopVo vo = null;
      ModelAndView mv = new ModelAndView("fix/storeshopedit");
      if(id != null){
          vo = storeShopService.getStoreShop(id);
          mv.addObject("actNm","更新");
      }else{
          vo = new StoreShopVo();
          vo.setId(0);
          mv.addObject("actNm","新增");
      }
      
      if(type == null || type == 0){
    	  mv.addObject("labelTitle", "经销商名称");
    	  mv.addObject("pageTitle", "经销商");
    	  vo.setType(0);
      }
      else{
    	  mv.addObject("labelTitle", "快修店名称");
    	  mv.addObject("pageTitle", "经销商");
    	  vo.setType(type);
      }
      mv.addObject("obj",vo);
      Map<String,ChinastatesVo> chinastatesMap =  chinastatesService.getAll();
      mv.addObject("areaMap", chinastatesMap);
      
      return mv;
  }
   
  
  @RequestMapping(value ="/store_shop_edit_submit.do")
  @ResponseBody
  public ResultModel udpate(StoreShopVo vo){
      return storeShopService.save(vo);
  }

   
   @RequestMapping(value ="/store_shop_del.do")
   @ResponseBody
   public ResultModel batchDelete(@RequestParam(value = "ids[]")Integer[] ids){
       return storeShopService.batchDelete(ids);
   }
}
